<?php

namespace app\modules\backend\controllers;

use Yii;
use app\tools\Re;
use app\models\Message;
use app\models\CustomerAppointment;

class AppointmentController extends BaseController
{
  public $state_name_arr = ['待服务','服务中','服务成功'];

  public function actionAppointmentList(){
    $page = Yii::$app->request->post('page', 0);
    $page_size = Yii::$app->request->post('page_size', 20);
    $shop_id = Yii::$app->request->post('shop_id', 0);
    $good_id = Yii::$app->request->post('good_id', 0);
    $name = Yii::$app->request->post('name', '');
    $phone = Yii::$app->request->post('phone', '');
    $state = Yii::$app->request->post('state', -2);
    $date_range = Yii::$app->request->post('created_at_range', '');
    $query = CustomerAppointment::find()->where(['status' => 1]);
    if (!empty($shop_id)) {
      $query->andWhere(['shop_id' => $shop_id]);
    }
    if (!empty($good_id)) {
      $query->andWhere(['good_id' => $good_id]);
    }
    if ($name) {
      $query->andWhere(['like', 'name', $name]);
    }
    if ($phone) {
      $query->andWhere(['like', 'phone', $phone]);
    }
    if ($state > -1) {
      $query->andWhere(['state' => $state]);
    }
    if ($date_range) {
      $data_arr = explode("-", $date_range);
      $start_at = $data_arr[0];
      $end_at = $data_arr[1] + 86400;
      $query->andWhere("order_info.created_at >= '$start_at' and order_info.created_at <= '$end_at'");
    }
    $list = $query->orderBy('created_at desc')->limit($page_size)->offset($page_size*$page)->all();
    $data = [];
    if ($list) {
      foreach($list as $v) {
        $data[] = [
          'id' => $v->id,
          'shop_id' => $v->shop_id,
          'shop_name' => $v->shop->name,
          'customer_id' => $v->customer_id,
          'customer_name' => $v->customer->name,
          'cate_id' => $v->cate_id,
          'cate_name' => $v->cate_id ? $v->goodCate->name : '',
          'good_id' => $v->good_id,
          'good_name' => $v->good_id ? $v->good->name : '',
          'name' => $v->name,
          'phone' => $v->phone,
          'service_address' => $v->area->parentArea->parentArea->name.$v->area->parentArea->name.$v->area->name.$v->service_address,
          'state' => $v->state,
          'state_name' => $this->state_name_arr[$v->state],
          'service_start_at_time' => $v->service_start_at ? date('Y-m-d H:i:s', $v->service_start_at) : '',
          'service_start_img' => $v->service_start_img,
          'service_finsh_time' => $v->service_finsh_at ? date('Y-m-d H:i:s', $v->service_finsh_at) : '',
          'service_finsh_img' => $v->service_finsh_img,
          'created_time' => date('Y-m-d H:i:s', $v->created_at)
        ];
      }
    }
    return Re::data([
      'data' => $data,
      'totalPage' => $query->count(),
      'currentPage' => $page,
      'perPage' => $page_size
    ]);
  }

  //预约服务状态变更
  public function actionChangeServiceState(){
    $id = Yii::$app->request->post('id', 0);
    $state = Yii::$app->request->post('state', 1);
    $img = Yii::$app->request->post('img', '');
    $appointment = CustomerAppointment::findOne($id);
    if (empty($appointment)) {
      return Re::data(null, '预约信息读取失败', Re::ERROR);
    }
    $appointment->state = $state;
    if ($state == 1) {
      $appointment->service_start_at = time();
      $appointment->service_start_img = $img;
    } else {
      $appointment->service_finsh_at = time();
      $appointment->service_finsh_img = $img;
    }
    $appointment->save();
    
    //保存消息
    $message = new Message();
    $message->shop_id = $appointment->shop_id;
    $message->customer_id = $appointment->customer_id;
    $message->title = $state == 5 ? '预约服务开始' : '预约服务完成';
    $message->content = '预约编号：'.$appointment->appointment_sn.($state == 1 ? '已开始服务' : '已完成服务');
    $message->created_at = time();
    $message->save();
    return Re::data();
  }
}
